/**
 * Created by MD-3 on 2017/5/27.
 */

function addAlarm() {
    var alarm = alarm_mock;
    var deviceId = alarm.id;
    var deviceName = alarm.name;
    var alarmId = alarm.alarmId;
    var alarmTime = alarm.alarmTime;
    var audioUrl = alarm.audioUrl;

    var device = getDeviceById(deviceId);
    var type = device.type;
    var alarmMarker = device.marker;
    var polyline = device.polyline;
    var circle = device.circle;
    var workState = device.workState;
    var alarmState = device.alarmState = "1";
    var updateState = device.updateState;

    //移动地图
    map.panTo(alarmMarker.getPosition());

    //报警音乐
    bjAlarmAmount = bjAlarmAmount == null ? 0 : bjAlarmAmount;
    play_bjAlarm();                                                       //报警音乐提醒

    //报警图标、颜色
    var icon = getMarkerImg(type, workState, alarmState, updateState);
    alarmMarker.setIcon(icon);                                           //报警图标
    var strokeColor = getStrokeColor(workState, alarmState, updateState);
    polyline.setOptions({strokeColor: strokeColor});
    circle.setOptions({strokeColor: strokeColor});
    circle.setOptions({fillColor: strokeColor});

    //跑马灯提醒
    var alarmInfoBar = document.getElementById("infoBar");
    var ulId = 'marquee-content-items';
    var liId = alarmId;
    var text = '<a style="color:#fff" onclick="mapPanTo(this)" deviceid=' + deviceId + '>发生报警：' + liId + ' ' + alarmTime + '</a>';
    addliById(ulId, liId, text);                                             //报警滚动消息

    //展开信息窗体
    var alarmCheck = "1";
    openDeviceInfo(device.id, alarmCheck);
}

var bjAlarmAmount;
function play_bjAlarm() {
    if (bjAlarmAmount == null) {
        return;
    }
    bjAlarmAmount++;
    var bjAlarm = document.getElementById("bj_alarm");
    if (bjAlarm) {
        bjAlarm.play();
    }
}

function stop_bjAlarm() {
    if (!bjAlarmAmount || 0 < (--bjAlarmAmount))    return;
    var bjAlarm = document.getElementById("bj_alarm");
    if (bjAlarm) {
        bjAlarm.pause();
    }
}

function addliById(ulId, liId, txt) {
    console.log(ulId+","+liId+","+txt)
    var ul = document.getElementById(ulId);
    var li = document.createElement("li");
    li.innerHTML = txt;
    li.id = liId;
    ul.appendChild(li);
}

function deleteliById(ulId, liId) {
    var ul = document.getElementById(ulId);
    var li = document.getElementById(liId);

    console.log(ulId+"************"+liId+"*************"+li)
    if (ul && li) {
        ul.removeChild(li);
    }
}

function playAlarmMedia(thisitem) {
    var thisItem = $(thisitem);
    var audioUrl = thisItem.attr("audioUrl");
    var count = thisItem.attr("count");
    var alarmMedia = document.getElementById("alarm-media");
    alarmMedia.src = audioUrl;

    if (count == '0') {
        alarmMedia.play();
        thisItem.attr("count", "1");
        thisItem.attr("src", resource_mock.IMG_ALARM_ON);
    } else {
        alarmMedia.pause;
        thisItem.attr("count", "0");
        thisItem.attr("src", resource_mock.IMG_ALARM_OFF);
    }
}
